package com.diligent.cloud.sfc.factory.impl;

import com.canny.commons.model.Codes;
import com.canny.commons.model.Result;
import com.canny.commons.page.IPage;
import com.canny.commons.page.Pagination;
import com.diligent.cloud.logistics.container.model.LcmBoxLedger;
import com.diligent.cloud.sfc.factory.StationFactory;
import com.diligent.cloud.sfc.model.detail.HangingDetailInfo;
import com.diligent.cloud.sfc.service.ISfcService;
import com.diligent.cloud.sfc.service.WorkShopExtrusionService;
import com.diligent.cloud.sfc.util.ConverterUtil;
import com.diligent.cloud.web.model.PageAndCondition;
import com.diligent.cloud.workorder.model.WoOperationCompleteDetail;
import com.diligent.cloud.workorder.model.WoOrder;
import com.diligent.cloud.workorder.model.WoOrderCompleteDetail;
import com.diligent.cloud.workorder.service.WoOrderCompleteDetailFeignService;
import com.diligent.foundation.data.model.v2.Condition;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * HangingStation class
 *
 * @author xf107278
 * @date 3/31/2020
 */
@Component
public class HangingStation implements StationFactory {

    private ISfcService sfcService;
    private WoOrderCompleteDetailFeignService orderCompleteDetailFeignService;
    private final WorkShopExtrusionService workShopExtrusionService;
    private final BoxStation boxStation;


    public HangingStation(ISfcService sfcService, WoOrderCompleteDetailFeignService orderCompleteDetailFeignService,
                          WorkShopExtrusionService workShopExtrusionService, BoxStation boxStation) {
        this.sfcService = sfcService;
        this.orderCompleteDetailFeignService = orderCompleteDetailFeignService;
        this.workShopExtrusionService = workShopExtrusionService;
        this.boxStation = boxStation;
    }

    @Override
    public Result getOrderInfo(WoOrder order) {
        HangingDetailInfo hangingDetailInfo = new HangingDetailInfo();

        //获取所有同工序工单,
        List<WoOrder> sameOrder = sfcService.getSameOrder(order.getMachineCode()
                , order.getOrderStatus()
                , order.getOrderType()
                , order.getSystemId()
                , order.getProduceDate());

        //完成生产
        Set<WoOrder> completeProduce = new HashSet<>();
        //完成挂料
        Set<WoOrder> completeHanging = new HashSet<>();
        //待挂数量
        Long waitHanging = sfcService.getOperationCompleteDetailByWoOrder(order.getWoOrder()).getDigit().longValue();
        //待挂同型号数量
        Long sameMatCodeWaitHanging = 0L;
        PageAndCondition pageAndCondition ;
        for (WoOrder woOrder : sameOrder) {
            //查询同型号工单
            if (order.getMatCode().equalsIgnoreCase(woOrder.getMatCode())) {
                //查询工单是否完工
                pageAndCondition = new PageAndCondition();
                pageAndCondition.setPagination(new Pagination(1L, 100000L));
                pageAndCondition.setConditions(Collections.singletonList(new Condition("woOrderId", "eq",
                        woOrder.getId(), null)));
                ResponseEntity<com.diligent.cloud.primary.data.model.Result<IPage<WoOrderCompleteDetail>>> orderCompleteDetailFeignPage =
                        orderCompleteDetailFeignService.page(pageAndCondition);
                if (orderCompleteDetailFeignPage.getBody()!=null && orderCompleteDetailFeignPage.getBody().getData().getRecords().size() != 0) {
                    //已完成
                    completeHanging.add(woOrder);
                } else {
                    //未完成 加判断是否剪切完成
                    WoOperationCompleteDetail operationCompleteDetailByWoOrder =
                            sfcService.getOperationCompleteDetailByWoOrder(woOrder.getWoOrder());
                    if (operationCompleteDetailByWoOrder!= null && operationCompleteDetailByWoOrder.getDigit()!=null){
                        completeProduce.add(woOrder);
                    }
                }
            }
        }
        //获取所有闲置框
        IPage<LcmBoxLedger> idleBox = sfcService.getIdleBox("null", 1L, 10000L);
        if (idleBox!=null) {
            hangingDetailInfo.setLoadingQuantityPerBox(idleBox.getRecords().stream().findFirst().get().getMatTotalLotDigit().longValue());
        }else {
            hangingDetailInfo.setLoadingQuantityPerBox(0L);
        }

        hangingDetailInfo.setCompleteProduce(completeProduce.size());
        hangingDetailInfo.setCompleteHanging(completeHanging.size());
        hangingDetailInfo.setWaitHanging(waitHanging);
        //查询本机台使用了多少框
        idleBox = sfcService.getIdleBox(order.getMachineCode(), 1L, 10000L);
        if (idleBox!=null){
            hangingDetailInfo.setWaitBoxNumber(idleBox.getTotal().intValue());
        }else {
            hangingDetailInfo.setWaitBoxNumber(0);
        }
        hangingDetailInfo.setUseBoxNum(sfcService.getLogisticsTransPortInfo(order.getMachineCode()).

                getRecords().

                size());
        //查工序完工单数量
        WoOperationCompleteDetail operationCompleteDetailByWoOrder =
                sfcService.getOperationCompleteDetailByWoOrder(order.getWoOrder());
        if(operationCompleteDetailByWoOrder!=null){
            hangingDetailInfo.setCompleteDigit(operationCompleteDetailByWoOrder.getDigit().longValue());
        }else {
            hangingDetailInfo.setCompleteDigit(0L);
        }
        hangingDetailInfo.setRequireDigit(order.getDeliveryDigit().longValue());
        Integer totalDigit = boxStation.getCompleteDigitByOrderId(order.getId());
        hangingDetailInfo.setHangingCompleteDigit(totalDigit);
        hangingDetailInfo.setWaitHanging(order.getDeliveryDigit().longValue()-totalDigit);
        for (WoOrder woOrder:completeProduce) {
            WoOperationCompleteDetail operationCompleteDetail =
                    sfcService.getOperationCompleteDetailByWoOrder(woOrder.getWoOrder());
            sameMatCodeWaitHanging += (operationCompleteDetail.getDigit().longValue() -  boxStation.getCompleteDigitByOrderId(woOrder.getId()));
        }
        hangingDetailInfo.setSameMatCodeWaitHanging(sameMatCodeWaitHanging);
        return ConverterUtil.ConverterByData(hangingDetailInfo, Codes.success.getCode(), Codes.success.getMsg());
    }
}
